import { Component, OnInit } from '@angular/core';
import { RegisterUser } from './register-user';
import { interval } from 'rxjs';
import { scan, take } from 'rxjs/operators';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.less']
})
export class RegisterComponent implements OnInit {

  formInfo: RegisterUser;
  isDisabled = false;
  btnText = '获取验证码';
  constructor() {
    this.formInfo = new RegisterUser();
  }

  ngOnInit() {
  }

  onRegister() {

  }

  sendCode() {
    if (!this.formInfo.phone) {
      return;
    }
    this.isDisabled = true;
    interval(1000).pipe(
      scan(i => i - 1, 60),
      take(60)
    ).subscribe(n => {
      if (n > 1) {
        this.btnText = `${n}s后重发`;
      } else {
        this.btnText = '获取验证码';
        this.isDisabled = false;
      }
    })

  }

}
